/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Library General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
*/

#include "stylemodel.h"

#include <QFile>
#include <QTextStream>
#include <QDomDocument>

#include "constantes.h"

StyleItem::StyleItem(QString name, QString font, QString size)
{
	styleName = name;
	styleFont = font;
    styleSize = size;
    styleColor = QColor(Qt::black);
    styleBold = false;
    styleItalic = false;
    styleUnderline = false;
    styleAlignment = 0;
    styleSelected = true;
}

StyleItem::StyleItem(QString name, QString font, QString size, int red, int green, int blue, bool bold, bool italic, bool underline, int alignment, bool selected)
{
    styleName = name;
    styleFont = font;
    styleSize = size;
    styleColor =  QColor(red, green, blue);
    styleBold = bold;
    styleItalic = italic;
    styleUnderline = underline;
    styleAlignment = alignment;
    styleSelected = selected;
}

StyleManager::StyleManager()
{
    styleList = new QList<StyleItem *>();

    readStyles();
}

StyleManager::~StyleManager()
{
	saveStyles();	
	
    StyleItem *item;
    foreach (item, *styleList)
		delete item;

    delete styleList;
}

QString StyleManager::getStyleName(int index)
{
    StyleItem *item = styleList->at(index);
    return item->styleName;
}

QString StyleManager::getStyleFont(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleFont;
}
QString StyleManager::getStyleSize(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleSize;
}
QColor StyleManager::getStyleColor(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleColor;
}
bool StyleManager::getStyleBold(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleBold;
}
bool StyleManager::getStyleItalic(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleItalic;
}
bool StyleManager::getStyleUnderline(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleUnderline;
}
int StyleManager::getStyleAlignment(int index)
{
	StyleItem *item = styleList->at(index);
    return item->styleAlignment;
}
bool StyleManager::isStyleSelected(int index)
{
	StyleItem *item = styleList->at(index);
	return item->styleSelected;
}

int StyleManager::getStyleIndexByName(QString name)
{
	StyleItem *item;
	for (int i = 0; i < styleList->count(); i++)
	{
		item = styleList->at(i);
		if (item->styleName == name)
			return i;
	}
	return -1;
}

int StyleManager::getStyleCount()
{
    return styleList->count();
}

void StyleManager::setStyleFont(int index, QString font)
{
	StyleItem *item = styleList->at(index);
    item->styleFont = font;
}
void StyleManager::setStyleSize(int index, QString size)
{
	StyleItem *item = styleList->at(index);
    item->styleSize = size;
}
void StyleManager::setStyleColor(int index, QColor color)
{
	StyleItem *item = styleList->at(index);
    item->styleColor = color;
}
void StyleManager::setStyleBold(int index, bool bold)
{
	StyleItem *item = styleList->at(index);
    item->styleBold = bold;
}
void StyleManager::setStyleItalic(int index, bool italic)
{
	StyleItem *item = styleList->at(index);
    item->styleItalic = italic;
}
void StyleManager::setStyleUnderline(int index, bool underline)
{
	StyleItem *item = styleList->at(index);
    item->styleUnderline = underline;
}
void StyleManager::setStyleAlignment(int index, int alignment)
{
	StyleItem *item = styleList->at(index);
    item->styleAlignment = alignment;
}
void StyleManager::setStyleSelected(int index, bool value)
{
	StyleItem *item = styleList->at(index);
	item->styleSelected = value;
}

bool StyleManager::doStyleExists(QString name)
{
	StyleItem *item;
	for (int i = 0; i < styleList->count(); i++)
	{
		item = styleList->at(i);
		if (item->styleName == name)
			return true;
	}
	return false;
}

int StyleManager::addStyle(QString name, QString font, QString size)
{
	int index = styleList->size();
	StyleItem *styleItem = new StyleItem(name, font, size);
	styleList->insert(index, styleItem);
    return index;
}

void StyleManager::updateModelWithCurrentList(QListWidget *list)
{
	int styleIndex;
	StyleItem *styleItem;
	QList<StyleItem *> *newList = new QList<StyleItem *>();
	QListWidgetItem *item;
	
	for (int i = 0; i < list->count(); i++)
	{
		item = list->item(i);
		styleIndex = item->data(Qt::UserRole).toUInt();
		styleItem = styleList->at(styleIndex);
		newList->append(styleItem);
	}
	
	delete styleList;
	styleList = newList;
}

void StyleManager::readStyles()
{
	/*
	#ifdef Q_WS_WIN
	//QString filename = "styles.xml";
	QString filename = QDir::toNativeSeparators(QDir::homePath() + "/Application Data/" + Constantes::APP_NAME + "/") + "styles.xml";
	#else
    QString filename = QDir::toNativeSeparators(QDir::homePath() + "/.config/" + Constantes::APP_NAME + "/") + "styles.xml";
    #endif
    */
    
    QString filename = Constantes::getConfigPath() + Constantes::STYLES_FILENAME;

    QDomDocument doc;
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();

    QDomElement root = doc.documentElement();
    QDomElement node = root.toElement();

    QString tagName;
    QString styleName;
    QString styleFont;
    QString styleSize;
    int styleRed;
    int styleGreen;
    int styleBlue;
    bool styleBold;
    bool styleItalic;
    bool styleUnderline;
    int styleAlignment;
    bool styleSelected;
    StyleItem *styleItem;

    // Loop on the nodes.
    while (!node.isNull())
    {
        QDomElement child = node.firstChildElement();
        while (!child.isNull())
        {
            tagName = child.tagName();
            if (tagName == Constantes::TAG_STYLE)
            {
                styleName = child.attribute(Constantes::TAG_NAME);
                styleFont = child.attribute(Constantes::TAG_FONT);
                styleSize = child.attribute(Constantes::TAG_SIZE);
                styleRed = child.attribute(Constantes::TAG_RED).toUInt();
                styleGreen = child.attribute(Constantes::TAG_GREEN).toUInt();
                styleBlue = child.attribute(Constantes::TAG_BLUE).toUInt();
                styleBold = (child.attribute(Constantes::TAG_BOLD) == "true") ? true : false;
                styleItalic = (child.attribute(Constantes::TAG_ITALIC) == "true") ? true : false;
                styleUnderline = (child.attribute(Constantes::TAG_UNDERLINE) == "true") ? true : false;
                styleAlignment = child.attribute(Constantes::TAG_ALIGNMENT).toUInt();
                styleSelected = ((child.attribute(Constantes::TAG_SELECTED) == "true") || (child.attribute(Constantes::TAG_SELECTED) == "")) ? true : false;

                styleItem = new StyleItem(styleName, styleFont, styleSize, styleRed, styleGreen, styleBlue, styleBold, styleItalic, styleUnderline, styleAlignment, styleSelected);
                styleList->append(styleItem);
            }
            child = child.nextSiblingElement();
        }
        node = node.nextSiblingElement();
    }
}

void StyleManager::saveStyles()
{
	/*
	#ifdef Q_WS_WIN
	QString filename = "styles.xml";
	QString filename = QDir::toNativeSeparators(QDir::homePath() + "/Application Data/" + Constantes::APP_NAME + "/") + "styles.xml";	
	#else
	QString filename = QDir::toNativeSeparators(QDir::homePath() + "/.config/" + Constantes::APP_NAME + "/") + "styles.xml";
	#endif
	*/
	QString filename = Constantes::getConfigPath() + Constantes::STYLES_FILENAME;
	
	QDomDocument doc;
	QDomElement element;
    QTextStream out;
	QFile file;
	StyleItem *styleItem;
	
	// Create the base element "styles".
    QDomElement root = doc.createElement(Constantes::TAG_STYLES);
    doc.appendChild(root);
	
	// Setup the file
    file.setFileName(filename);
    if (!file.open(QIODevice::WriteOnly))
        return;        
    out.setDevice(&file);
    
    for (int i = 0; i < styleList->count(); i++)
    {
    	styleItem = styleList->at(i);
    	element = doc.createElement(Constantes::TAG_STYLE);
		root.appendChild(element);     
		element.setAttribute(Constantes::TAG_NAME, styleItem->styleName);
		element.setAttribute(Constantes::TAG_FONT, styleItem->styleFont);
		element.setAttribute(Constantes::TAG_SIZE, styleItem->styleSize);
		element.setAttribute(Constantes::TAG_RED, styleItem->styleColor.red());
		element.setAttribute(Constantes::TAG_GREEN, styleItem->styleColor.green());
		element.setAttribute(Constantes::TAG_BLUE, styleItem->styleColor.blue());
		element.setAttribute(Constantes::TAG_BOLD, styleItem->styleBold ? "true" : "false");
		element.setAttribute(Constantes::TAG_ITALIC, styleItem->styleItalic ? "true" : "false");
		element.setAttribute(Constantes::TAG_UNDERLINE, styleItem->styleUnderline ? "true" : "false");
		element.setAttribute(Constantes::TAG_ALIGNMENT, styleItem->styleAlignment);
		element.setAttribute(Constantes::TAG_SELECTED, styleItem->styleSelected ? "true" : "false");
   	}
    
    // Insert <?xml version="1.0" ?> at the begining of the document.
    QDomNode node = doc.createProcessingInstruction("xml","version=\"1.0\"");
    doc.insertBefore(node, doc.firstChild());
    // write the file.
    doc.save(out, 2);
    file.close();	
}

/*
void StyleManager::updateComboStyle(QComboBox *comboStyle)
{
	StyleItem *styleItem;
	QString currentText;
	
	if (comboStyle->count() > 0)
		currentText = comboStyle->currentText();
	else
		currentText = "";
	
	comboStyle->clear();
	for (int i = 0; i < styleList->count(); i++)
	{
		styleItem = styleList->at(i);
		comboStyle->addItem(styleItem->styleName, i);
	}
	
	if (currentText != "")
	{
		int index = comboStyle->findText(currentText);
		if (index != -1)
			comboStyle->setCurrentIndex(index);
		else
			comboStyle->setCurrentIndex(0);
	}
	else
		comboStyle->setCurrentIndex(0);
}
*/
